package com.moji.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.base.ApiResult;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.*;
import com.moji.entity.dto.SearchDto;
import com.moji.entity.dto.WeatherDto;
import com.moji.entity.vo.CommonBasicDTO;
import com.moji.entity.vo.SearchVo;
import com.moji.entity.vo.WeatherVo;
import com.moji.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 网站底部基本信息表(MjBasicInfo)表控制层
 *
 * @author makejava
 * @since 2024-07-21 14:35:51
 */
@RestController
@RequestMapping("web/mjBasicInfo")
@Api(tags = "基础信息web接口", value = "基础信息web接口")
public class MjBasicInfoController {
    /**
     * 服务对象
     */
    @Resource
    private MjBasicInfoService mjBasicInfoService;
    @Resource
    private MjSolutionService mjSolutionService;
    @Resource
    private MjProductService mjProductService;
    @Resource
    private MjNewsService mjNewsService;
    @Resource
    private MjSupportHelpService mjSupportHelpService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("/queryByPage")
    @ApiOperation(value = "全部网站基本信息 - 分页查询(只需要传入分页参数)", response = MyPage.class)
    public ApiResult<MyPage<MjBasicInfo>> queryByPage(@RequestBody CommonBasicDTO commonBasicDTO) {
        MyPage<MjBasicInfo> mjBannerMyPage = mjBasicInfoService.queryByPage(commonBasicDTO);
        return ApiResult.ok(mjBannerMyPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据id查看基础信息详情",response = MjBasicInfo.class)
    public ApiResult<MjBasicInfo> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjBasicInfoService.queryById(id));
    }
    @PostMapping("/weather")
    @ApiOperation(value = "根据经纬度查看天气",response = WeatherVo.class)
    public WeatherVo queryWeather(@RequestBody WeatherDto weatherDto) {
        WeatherVo weatherVo=  this.mjBasicInfoService.queryWeather(weatherDto);
        return weatherVo;
    }
    @PostMapping("/search")
    @ApiOperation(value = "全局搜索")
    public  ApiResult<SearchVo> search(@RequestBody SearchDto searchDto) {
       SearchVo searchVo=new SearchVo();
       searchVo.setSupportSize(0);
       searchVo.setSolutionSize(0);
       searchVo.setProductionSize(0);
       searchVo.setNewsSize(0);
        LambdaQueryWrapper<MjSolution> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(MjSolution::getSolutionName,searchDto.getSearch())
                .or()
                .like(MjSolution::getContentDesc,searchDto.getSearch());
        List<MjSolution> list = mjSolutionService.list(wrapper);
        if(list!=null)
        {
            searchVo.setSolutionSize(list.size());
            PageHelper.startPage(searchDto.getPageNum(), searchDto.getPageSize());
            PageInfo<MjSolution> info = new PageInfo<>(list);
            MyPage<MjSolution> mjSolutionMyPage = PageUtil.PageInfo2MyPage(info);
            searchVo.setSolutionList(mjSolutionMyPage);
        }
        LambdaQueryWrapper<MjProduct> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.like(MjProduct::getProductName,searchDto.getSearch())
                .or()
                .like(MjProduct::getContentDesc,searchDto.getSearch());
        List<MjProduct> list1 = mjProductService.list(wrapper1);
        if(list1!=null)
        {
            searchVo.setProductionSize(list1.size());
            PageHelper.startPage(searchDto.getPageNum(), searchDto.getPageSize());
            PageInfo<MjProduct> info1 = new PageInfo<>(list1);
            MyPage<MjProduct> mjProductMyPage = PageUtil.PageInfo2MyPage(info1);
            searchVo.setProductList(mjProductMyPage);
        }
        LambdaQueryWrapper<MjNews> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.like(MjNews::getTitle,searchDto.getSearch())
                .or()
                .like(MjNews::getContent,searchDto.getSearch());
        List<MjNews> list2 = mjNewsService.list(wrapper2);
        if(list2!=null)
        {
            searchVo.setNewsSize(list2.size());
            PageHelper.startPage(searchDto.getPageNum(), searchDto.getPageSize());
            PageInfo<MjNews> info2 = new PageInfo<>(list2);
            MyPage<MjNews> mjNewsMyPage = PageUtil.PageInfo2MyPage(info2);
            searchVo.setNewsList(mjNewsMyPage);
        }
        LambdaQueryWrapper<MjSupportHelp> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.like(MjSupportHelp::getQuestionName,searchDto.getSearch())
                .or()
                .like(MjSupportHelp::getAnswer,searchDto.getSearch())
                .or()
                .like(MjSupportHelp::getDocumentName,searchDto.getSearch());
        List<MjSupportHelp> list3 = mjSupportHelpService.list(wrapper3);
        if(list3!=null)
        {
            searchVo.setSupportSize(list3.size());
            PageHelper.startPage(searchDto.getPageNum(), searchDto.getPageSize());
            PageInfo<MjSupportHelp> info3 = new PageInfo<>(list3);
            MyPage<MjSupportHelp> mjSupportHelpMyPage = PageUtil.PageInfo2MyPage(info3);
            searchVo.setSupportList(mjSupportHelpMyPage);
        }
        return ApiResult.ok(searchVo);
    }
}

